<!--

施工审查延误影响分析

-->
<template>
  <my-dialog :subLoading="subLoading" @on-submit="submitHandle" :readOnly="readOnly">
    <my-header slot="header" project-sub-text="延误影响分析"  :project-sub-title="`施工单位检查日期: ${constructionExamine.created_at||''}`" :project-name="projectDetail.project_name || ''"></my-header>
    <div slot="body">
      <my-textarea v-model="constructionExamine.analyze" :disabled="true" label-text="进度分析" placeholder="请输入进度分析" error-message="请输入进度分析"></my-textarea>
      <my-textarea v-model="constructionExamine.improvement_measures" :disabled="true" label-text="进度纠偏措施" placeholder="请输入进度纠偏措施" error-message="请输入进度纠偏措施"></my-textarea>
      <check-box v-model="constructionExamine.modificationBoole" :disabled="true" lbl-text="是否调整进度计划"></check-box>
      <widgets icon="times" class="widget-header" title="监理单位延误影响分析" :value="completeTimeDiff || ''"></widgets>
      <my-textarea v-model="pscAnalyze" :disabled="readOnly" label-text="进度分析" placeholder="请输入进度分析" error-message="请输入进度分析"></my-textarea>
      <my-textarea v-model="advice" :disabled="readOnly" label-text="进度纠偏措施相关建议" placeholder="请输入进度纠偏措施相关建议" error-message="请输入进度纠偏措施相关建议"></my-textarea>
      <my-textarea v-model="costAffect" :disabled="readOnly" label-text="措施对工程质量安全成本的影响" placeholder="请输入措施对工程质量安全成本的影响" error-message="请输入措施对工程质量安全成本的影响"></my-textarea>
      <check-box v-model="available" :disabled="readOnly" lbl-text="纠偏措施是否可行"></check-box>
    </div>
  </my-dialog>
</template>

<script>
  import Dialog from "@/components/Dialog";
  import Header from "@/components/Dialog/Header";
  import Widgets from "@/components/Dialog/Widgets";
  import TextArea from "@/components/Dialog/TextArea";
  import CheckBox from "@/components/Dialog/Check";
  import  { mapGetters } from "vuex";
  import _ from 'lodash'
  import taskApi from '@/api/task'
  import projectApi from '@/api/project'
  import moment from 'moment'
  moment.locale('zh-CN')

  export default {
    computed: {
      ...mapGetters([
        'userData'
      ]),
      completeTimeDiff () {
        if (this.taskDetail.do_task_at && this.taskDetail.status === 0) {
          const complateTime = moment(this.taskDetail.do_task_at).add(2, 'days')
          const diffHours = complateTime.diff(moment(), 'hours')
          const diffMin = complateTime.subtract(diffHours, 'hours').diff(moment(), 'minutes')
          if (diffMin < 0) {
            return `已超过处理时间${-diffHours}小时${-diffMin}分钟`
          }
          return `距离最后的处理时间剩余：${diffHours}小时${diffMin}分钟`
        }
        if (this.taskDetail.status === 1) {
          return `已处理，上报日期：${this.masterCreated}`
        }
        return ''
      }
    },
    data () {
      return {
        readOnly: false,
        subLoading: false,
        pscAnalyze: '',
        advice: '',
        costAffect: '',
        available: true,
        constructionExamine: {
          created_at: '',
          analyze: '',
          improvement_measures: '',
          modificationBoole: true
        },
        projectDetail: {
          file: []
        },
        taskDetail: {},
        masterCreated: ''
      }
    },
    components: {
      'MyDialog': Dialog,
      'MyHeader': Header,
      Widgets,
      'MyTextarea': TextArea,
      CheckBox
    },
    created () {
      this.getProjectInfo()
      this.getTaskInfo(() => {
        this.getCheckPlanDetail()
      })
    },
    methods: {
      getProjectInfo () {
        const projectId = this.$route.params.projectId
        this.$store.dispatch('getSingeProject', {token: this.userData.token, pId: projectId}).then(res => {
          this.projectDetail = res.data
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      },
      getTaskInfo (cb) {
        const taskId = this.$route.params.taskId
        taskApi.getTaskInfo(this.userData.token, taskId).then(res => {
          const result = res.data
          this.taskDetail = result.data
          if (cb) {
            cb()
          }
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      },
      getCheckPlanDetail () {
        const checkPlanId = this.taskDetail.check_plan_id
        projectApi.getCheckPlanDetail(this.userData.token, checkPlanId).then(res => {
          const result = res.data.data
          this.constructionExamine = result.constructorAnalyzeCause.data
          this.constructionExamine.modificationBoole = (this.constructionExamine.is_modification === 0)
          if (result.taskmasterAnalyzeExamine.data.id) {
            this.readOnly = true
            this.masterCreated = result.taskmasterAnalyzeExamine.data.created_at
            this.pscAnalyze = result.taskmasterAnalyzeExamine.data.analyze
            this.advice = result.taskmasterAnalyzeExamine.data.suggest
            this.costAffect = result.taskmasterAnalyzeExamine.data.influence
            this.available = (result.taskmasterAnalyzeExamine.data.is_approve === 0)
          }
        })
      },
      submitHandle () {
        if (!this.pscAnalyze) {
          this.$message.error('请输入进度分析')
          return
        }
        if (!this.advice) {
          this.$message.error('请输入进度纠偏措施相关建议')
          return
        }
        if (!this.costAffect) {
          this.$message.error('请输入措施对工程质量安全成本的影响')
          return
        }
        this.subLoading = true
        const projectId = this.$route.params.projectId
        const taskId = this.$route.params.taskId
        const checkPlanId = this.taskDetail.check_plan_id
        taskApi.superviseAnalyze(this.userData.token, {
          task_id: taskId,
          project_id: projectId,
          check_plan_id: checkPlanId,
          is_approve: this.available ? 0 : 1,
          users_id: this.projectDetail.users_id,
          analyze: this.pscAnalyze,
          suggest: this.advice,
          influence: this.costAffect
        }).then(res => {
          this.$message.success('提交成功')
          this.subLoading = false
          this.$router.back()
        }, (res) => {
          this.$message(`数据请求失败,错误信息:${JSON.stringify(res)}`)
        }).catch(err => {
          this.$message(`系统错误，请求数据错误，错误信息:${JSON.stringify(err)}`)
        })
      }
    }
  }
</script>
